﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace EggHarvest.GraphicsSupport
{
    static public class Camera
    {
        static private Vector2 sOrigin = Vector2.Zero;      // Origin of the world
        static private float sWidth = 100f;                 // Width of the world
        static private float sRatio = -1f;                  // Ratio between camera window and pixel 
        static private float sHeight = -1f;

        /// Accessors to the camera window bounds
        static public Vector2 CameraWindowLowerLeftPosition
            { get { return sOrigin; } }
        static public Vector2 CameraWindowUpperRightPosition
            { get { return sOrigin + new Vector2(sWidth, sHeight); } }

        static private float cameraWindowToPixelRatio()
        {
            if (sRatio < 0f)
            {
                sRatio = (float)Game1.sGraphics.PreferredBackBufferWidth / sWidth;
                sHeight = sWidth * (float)Game1.sGraphics.PreferredBackBufferHeight / (float)Game1.sGraphics.PreferredBackBufferWidth;
            }
            
            return sRatio;
        }

        static public void SetCameraWindow(Vector2 origin, float width)
        {
            sOrigin = origin;
            sWidth = width;
        }

        static public void ComputePixelPosition(Vector2 cameraPosition, out int x, out int y)
        {
            float ratio = cameraWindowToPixelRatio();
            // Convert the position to pixel space
            x = (int)(((cameraPosition.X - sOrigin.X) * ratio) + 0.5f);
            y = (int)(((cameraPosition.Y - sOrigin.Y) * ratio) + 0.5f);
            y = Game1.sGraphics.PreferredBackBufferHeight - y;
        }

        static public Rectangle ComputePixelRectangle(Vector2 position, Vector2 size)
        {
            float ratio = cameraWindowToPixelRatio();
            // Convert size from camera window space to pixel space.
            int width = (int)((size.X * ratio) + 0.5f);
            int height = (int)((size.Y * ratio) + 0.5f);
            // Convert the position to pixel space
            int x, y;
            ComputePixelPosition(position, out x, out y);
            // Reference position is the center
            y -= height / 2;
            x -= width / 2;
            return new Rectangle(x, y, width, height);
        }

        // Support collision with the camera bounds
        public enum CameraWindowCollisionStatus
        {
            CollideTop = 0,
            CollideBottom = 1,
            CollideLeft = 2,
            CollideRight = 3,
            InsideWindow = 4
        };

        static public CameraWindowCollisionStatus CollidedWithCameraWindow(TexturedPrimitive prim)
        {
            //Vector2 min = new Vector2(0,0);
            Vector2 min = CameraWindowLowerLeftPosition;
            Vector2 max = CameraWindowUpperRightPosition;
            
            if (prim.MaxBound.Y > max.Y)
                return CameraWindowCollisionStatus.CollideTop;
            if (prim.MinBound.X < min.X)
                return CameraWindowCollisionStatus.CollideLeft;
            if (prim.MaxBound.X > max.X)
                return CameraWindowCollisionStatus.CollideRight;
            if (prim.MinBound.Y < min.Y)
                return CameraWindowCollisionStatus.CollideBottom;
            
            return CameraWindowCollisionStatus.InsideWindow;
        }

        static public Vector2 RandomPosition()
        {
            float rangeX = 0.8f * sWidth;
            float offsetX = 0.1f * sWidth;
            float rangeY = 0.8f * sHeight;
            float offsetY = 0.1f * sHeight;

            int status = Game1.sRan.Next(1, 4);
            float x = 0;

            if (Game1.aStatus == 1)
            {
                if(Game1.sDuck==false && Game1.sCheese==false)
                    x = 85f;
                if (Game1.sDuck)
                {
                    if (status == 2) x = 55f;
                    if (status == 1) x = 85f;
                    if (status == 3) x = 85f;
                }
                if (Game1.sCheese)
                {
                    if (status == 2) x = 25f;
                    if (status == 1) x = 85f;
                    if (status == 3) x = 85f;
                }
                if (Game1.sDuck && Game1.sCheese)
                {
                    if (status == 1) x = 25f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 85f;
                }
            }
           

            if (Game1.aStatus==3)
            {
                if (Game1.sChicken == false && Game1.sDuck == false)
                {
                    x = 25f;
                }
                if (Game1.sDuck)
                {
                    if (status == 1) x = 25f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 25f;
                }
                if (Game1.sChicken)
                {
                    if (status == 1) x = 25f;
                    if (status == 2) x = 25f;
                    if (status == 3) x = 85f;
                }
                if(Game1.sChicken&&Game1.sDuck)
                {
                    if (status == 1) x = 25f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 85f;
                }
                
            }

            if (Game1.aStatus == 2)
            {
                if (Game1.sChicken==false && Game1.sCheese==false)
                {
                    x = 55f;
                }

                if(Game1.sChicken)
                {
                    if (status == 1) x = 85f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 85f;
                }

                if (Game1.sCheese)
                {
                    if (status == 1) x = 55f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 25f;
                }

                if (Game1.sChicken && Game1.sCheese)
                {
                    if (status == 1) x = 25f;
                    if (status == 2) x = 55f;
                    if (status == 3) x = 85f;
                }
                
            }

            return new Vector2(x, 70);
        }
    }


}
